﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;

namespace Hangout.Web.Core.Score
{
    public class UserScore
    {
        public static Model.UserScore IncreaseUserScore(int userId, int scoreInc,ref Model.HangoutDBEntities context)
        {
            Model.UserScore score = GetLastScore(userId, ref context);
            if (score.CurrentScore+scoreInc >= 100)
            {
                return InsertScore(userId, 99, scoreInc, ref context);
            }

            return InsertScore(userId, score.CurrentScore + scoreInc, scoreInc, ref context);
        }

        public static Model.UserScore DecreaseUserScore(int userId, int scoreDec, ref Model.HangoutDBEntities context)
        {
            Model.UserScore score = GetLastScore(userId, ref context);
            if (score.CurrentScore - scoreDec <19)
            {
                return InsertScore(userId, 19,-scoreDec, ref context);
            }

           return  InsertScore(userId, score.CurrentScore - scoreDec, -scoreDec, ref context);

        }

        public static Model.UserScore GetLastScore(int userId, ref Model.HangoutDBEntities context)
        {
            Model.UserScore score = context.UserScores.Where(o => o.UserID == userId).SingleOrDefault();
            if (score == null)
            {
                return InsertDefaultScore(userId, ref context);
            }

            return score;
        }

        public static Model.UserScore InsertDefaultScore(int userId, ref Model.HangoutDBEntities context)
        {
            Model.UserScore sc = new Model.UserScore();
            sc.UserID = userId;
            sc.ScoreChange = 0;
            sc.CurrentScore = 50;
            sc.DateTimeStamp = DateTime.Now;
            context.AddToUserScores(sc);
            context.SaveChanges();

            return sc;
        }

        public static Model.UserScore InsertScore(int userId,int score,int scoreChange,ref Model.HangoutDBEntities context)
        {
            Model.UserScore sc=GetLastScore(userId,ref context);
            bool IF = false; //Insert Flag
            if (sc == null)
            {
                IF = true;
                sc = new Model.UserScore();
                sc.UserID = userId;
                sc.ScoreChange = 0;
                sc.CurrentScore = score;

            }

            sc.ScoreChange = 0;
            sc.CurrentScore = score;
            sc.DateTimeStamp = DateTime.Now;

            if (IF)
            {
                context.AddToUserScores(sc);

            }

            context.SaveChanges();

            return sc;

        }

        public static List<Model.User> GetInActiveUsers(ref Model.HangoutDBEntities context)
        {
            return context.UserScores.Where(o => EntityFunctions.DiffDays(DateTime.Now, o.DateTimeStamp) >= 7).Select(o => o.User).ToList();
        }

    }
}